<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/16
 * Time: 15:51
 */

namespace app\admin\controller;


use app\common\controller\ReturnCode;
use Qcloud\Cos\Client;
use think\Controller;
use think\Request;
use  app\admin\model\Goods as G;
use  app\admin\model\GoodsSpec as GS;
use app\admin\model\GoodsType as GT;
class Goods extends Controller
{
//    private $aid;
//    private $token;
//
    private $hasToken;
    function __construct(Request $request = null)
    {
        parent::__construct($request);

        $this -> aid = $request -> param('aid');
        $this -> token = $request -> param('token');
        $this -> page = $request -> param('page');
        $this -> g_name = $request -> param('g_name');
        $this -> gid = $request -> param('gid');
        $this -> typeId = $request -> param('type_id');
        $this -> spec = $request -> param('hasSpec',0,'trim');
        $this -> hasToken = hasToken($this ->aid , $this ->token);

    }
    //列表
    public function index(){

        if(!isset($this -> aid) || empty($this -> token)){
            return json(['status' => 2,'msg' => ReturnCode::$return_code[2]]);
        }
//        dump($this -> token);
//        die;
        if($this -> hasToken){
            $res = G::where('goods_id','>',0)->paginate(10,false,['page' => $this -> page]);
//            dump($res);
            foreach ($res as $key => $val){
                $GS = new GS();
                $val['spec'] = $GS::where('goods_id', '=', $val['goods_id'])->field("spec,feed,dregs,sugar,flavor,state,tem")->find();
                if(!empty($val['spec']['spec'])){
                    $str = $val['spec']['spec'];
                    $arr = explode(',',$str);
                    foreach ($arr as $k => $v){
//                        dump($k);
                        $arr[$k] = db('set_spec')->field('spec_name')->where('spec_id','=',$v)->find();
//                        dump($arr[$k]);
                    }
//                    dump(reduceArray($arr));
                    $val['spec']['spec'] = implode(',',reduceArray($arr));
//                    $val['spec']['spec'] = reduceArray($arr);
                }
                if(!empty($val['spec']['feed'])){
                    $str = $val['spec']['feed'];
                    $arr = explode(',',$str);
                    foreach ($arr as $k => $v){
////                        dump($k);
                        if(strpos($v,'¥') !== false){
                            $index = trim(substr($v,0,strpos($v,'¥')));
                        }else{
                            $index = $v ;
                        }

                        $arr[$k] = db('set_feed')->field('feed_name')->where('feed_id','=',$index)->find();
                        $arr[$k]['price'] = trim(strrchr($v,'¥'),'¥');
                        $arr[$k] = implode('+¥',$arr[$k]);
                    }
//                    dump(implode(',',$arr));
                    $val['spec']['feed'] = implode(',',$arr);
//                    $val['spec']['spec'] = implode(',',$arr);

                }
                if(!empty($val['spec']['dregs'])){
                    $str = $val['spec']['dregs'];
                    $arr = explode(',',$str);
                    foreach ($arr as $k => $v){
//                        dump($k);
                        $arr[$k] = db('set_dregs')->field('dregs_name')->where('dregs_id','=',$v)->find();
                    }
//                    $val['spec']['dregs'] = $arr;
                    $val['spec']['spec'] = implode(',',reduceArray($arr));

                }
                if(!empty($val['spec']['sugar'])){
                    $str = $val['spec']['sugar'];
                    $arr = explode(',',$str);
                    foreach ($arr as $k => $v){
//                        dump($k);
                        $arr[$k] = db('set_sugar')->field('sugar_name')->where('sugar_id','=',$v)->find();
                    }
//                    $val['spec']['sugar'] = $arr;
                    $val['spec']['sugar'] = implode(',',reduceArray($arr));;

                }
                if(!empty($val['spec']['flavor'])){
                    $str = $val['spec']['flavor'];
                    $arr = explode(',',$str);
                    foreach ($arr as $k => $v){
//                        dump($k);
                        $arr[$k] = db('set_flavor')->field('flavor_name')->where('flavor_id','=',$v)->find();
                    }
//                    $val['spec']['flavor'] = $arr;
                    $val['spec']['flavor'] = implode(',',reduceArray($arr));

                }
                if(!empty($val['spec']['state'])){
                    $str = $val['spec']['state'];
                    $arr = explode(',',$str);
                    foreach ($arr as $k => $v){
//                        dump($k);
                        $arr[$k] = db('set_state')->field('state_name')->where('state_id','=',$v)->find();
                    }
//                    $val['spec']['state'] = $arr;
                    $val['spec']['state'] = implode(',',reduceArray($arr));

                }
                if(!empty($val['spec']['tem'])){
                    $str = $val['spec']['tem'];
                    $arr = explode(',',$str);
                    foreach ($arr as $k => $v){
//                        dump($k);
                        $arr[$k] = db('set_tem')->field('tem_name')->where('tem_id','=',$v)->find();
                    }
//                    $val['spec']['tem'] = $arr;
                    $val['spec']['tem'] = implode(',',reduceArray($arr));

                }
//                dump($val['spec']);
                $res[$key] = $val;
            }

//            die;
            if($res){
                return json(['status' => 0,'msg' => ReturnCode::$return_code[0],'data' => $res]);
            }
            return json(['status' => 1,'msg' => ReturnCode::$return_code[1]]);
        }
        return json(['status' => 3,'msg' => ReturnCode::$return_code[3]]);
    }
    //添加
    public function addgoods(Request $request){
        $spec_data = $request->param();
        $img = $request->file('img');
//        dump($img);
//        die;
        $cosClient = new Client(array(
            'region' => 'ap-chengdu', #地域，如ap-guangzhou,ap-beijing-1
            'credentials' => array(
                'secretId' => 'AKIDGR5mN0RSWQHyX1sjOEMSZdqwtrkYdIxR', 'secretKey' => '10reGpPpDVlz8Wr255zkrv6COTOOU2eK',
            ))
        );
        if(!isset($this -> aid) || empty($this -> token) || empty($this -> g_name) || empty($img) || empty($this -> typeId)){
            return json(['status' => 2,'msg' => ReturnCode::$return_code[2]]);
        }
//        dump($this -> spec);
//        die;
        if($this -> hasToken){
            $data = ['g_name' => $this -> g_name,'type_id' => $this -> typeId];
            $has = G::where($data)->find();
            if($has){
               return json(['status' => 23, 'msg' => ReturnCode::$return_code[23]]);
            }
            /**
             * 图片处理
             */
            if($img){
                $res_img = upload_img($cosClient, $img, 'goods_img');
                if ($res_img->getData('code') == 1) {
                    return json(['status' => 1002, 'msg' => ReturnCode::$return_code['1002']]);
                }
                $res_data = $res_img->getData('data');
                $data['g_img'] = $res_data['data']['src'];
            }
            $data['g_spec'] = $this -> spec;
            $res = G::create($data);
            $goodId = G::getLastInsID();
            if($res){
                if($this -> spec  == 1){
                    $spec_data['goods_id'] = $goodId;
                    $GS = new GS();
                    $GS->allowField(true)->save($spec_data);
                }
                return json(['status' => 0,'msg' => ReturnCode::$return_code[0]]);
            }
            return json(['status' => 1,'msg' => ReturnCode::$return_code[1]]);
        }
        return json(['status' => 3,'msg' => ReturnCode::$return_code[3]]);
    }

    //规格控制
//    private function specgoods(){
//
//    }

    //详情
    public function goodsinfo(){
        if(!isset($this -> aid) || empty($this -> token) || empty($this -> gid)){
            return json(['status' => 2,'msg' => ReturnCode::$return_code[2]]);
        }
        if($this -> hasToken){
           $where = ['goods_id' => $this -> gid];
            $res = G::where($where)->find();
            $res['spec'] = GS::where($where)->find();
//            $type = GT::where('type_id','=',$res['type_id']) -> find();
            if($res){
                return json(['status' => 0,'msg' => ReturnCode::$return_code[0],'data' => $res]);
            }
            return json(['status' => 1,'msg' => ReturnCode::$return_code[1]]);
        }
        return json(['status' => 3,'msg' => ReturnCode::$return_code[3]]);
    }
    //修改
    public function goodsup(Request $request){
        $data = $request->param();
        $data['goods_id'] = $this -> gid;
        $img = $request->file('img');
        $cosClient = new Client(array(
                'region' => 'ap-chengdu', #地域，如ap-guangzhou,ap-beijing-1
                'credentials' => array(
                    'secretId' => 'AKIDGR5mN0RSWQHyX1sjOEMSZdqwtrkYdIxR', 'secretKey' => '10reGpPpDVlz8Wr255zkrv6COTOOU2eK',
                ))
        );
        if(!isset($this -> aid) || empty($this -> token) || empty($this -> gid) ||empty($this -> g_name) || empty($this -> typeId)){
            return json(['status' => 2,'msg' => ReturnCode::$return_code[2]]);
        }
        if($this -> hasToken){
            $hasWhere = ['g_name' => $this ->g_name,'type_id' => $this -> typeId];
            $has = G::where($hasWhere)->find();
            if($has){
                return json(['status' => 23,'msg' => ReturnCode::$return_code[23]]);
            }

            /**
             * 图片上传
             */
            if($img){
                $res_img = upload_img($cosClient, $img, 'goods_img');
                if ($res_img->getData('code') == 1) {
                    return json(['status' => 1002, 'msg' => ReturnCode::$return_code['1002']]);
                }
                $res_data = $res_img->getData('data');
                $data['g_img'] = $res_data['data']['src'];
            }
//            dump($data);
//            die;
            $data['g_spec'] = $this -> spec;
            $where = ['goods_id' => $this -> gid];
            $res = G::update($data,$where)->allowField(true);
            if($res){
                $GS = new GS();
                $GS->where($where)->delete();
                if($this -> spec == 1){
                    $GS->allowField(true)->save($data);
                }
                return json(['status' => 0,'msg' => ReturnCode::$return_code[0]]);
            }
            return json(['status' => 1,'msg' => ReturnCode::$return_code[1]]);
        }
        return json(['status' => 3,'msg' => ReturnCode::$return_code[3]]);
    }
    //删除
    public function goodsdel(){
        if(!isset($this -> aid) || empty($this -> token) || empty($this -> gid)){
            return json(['status' => 2,'msg' => ReturnCode::$return_code[2]]);
        }
        if($this -> hasToken){
            $where = ['goods_id' => $this -> gid];
            $has = G::where($where)->find();
            if(!$has){
                return json(['status' => 23,'msg' => ReturnCode::$return_code[23]]);
            }
            $res = G::destroy($where);
            if($res){
                return json(['status' => 0,'msg' => ReturnCode::$return_code[0]]);
            }
            return json(['status' => 1,'msg' => ReturnCode::$return_code[1]]);
        }
        return json(['status' => 3,'msg' => ReturnCode::$return_code[3]]);
    }

    /**
     * 添加页规格选项
     */
    public function addpage(){

        if(!isset($this -> aid) || empty($this -> token)){
            return json(['status' => 2,'msg' => ReturnCode::$return_code[2]]);
        }
        if($this -> hasToken){
            $res = [];
//            $field = 'id,name';
            $res['type'] = GT::where('type_id','>',0)->field('type_id,type_name')->select();
            $res['spec'] = db('set_spec')->field('spec_id,spec_name')->select(); //规格
            $res['feed'] = db('set_feed')->field('feed_id,feed_name')->select(); //规格
            $res['dregs'] = db('set_dregs')->field('dregs_id,dregs_name')->select(); //规格
            $res['sugar'] = db('set_sugar')->field('sugar_id,sugar_name')->select(); //规格
            $res['flavor'] = db('set_flavor')->field('flavor_id,flavor_name')->select(); //规格
            $res['state'] = db('set_state')->field('state_id,state_name')->select(); //规格
            $res['tem'] = db('set_tem')->field('tem_id,tem_name')->select(); //规格

            if($res){
                return json(['status' => 0,'msg' => ReturnCode::$return_code[0],'data'=>$res]);
            }
            return json(['status' => 1,'msg' => ReturnCode::$return_code[1]]);
        }
        return json(['status' => 3,'msg' => ReturnCode::$return_code[3]]);
    }


    /**
     * 规格
     *
     *  button  ,
     * 规格(string)
     * 加料(string) ¥ 区分
     * 茶底(string)
     * 糖度(string)
     * 口味(string)
     * 状态(string)
     * 温度(string)
     */
//    public function specgood(Request $request){
//        $goods_id = $request -> param('goods_id');
////        if()
////        dump($request->param());
//        $GS = new GS();
//        $res = $GS->allowField(true)->save($request->param());
//        if($res){
//            return json(['status' => 0,'msg' => ReturnCode::$return_code[0]]);
//        }
//        return json(['status' => 1,'msg' =>     ReturnCode::$return_code[1]]);
//
//    }

}